package com.quanyan.assets.task.tracker;

import com.github.ltsopensource.core.domain.Action;
import com.github.ltsopensource.core.domain.Job;
import com.github.ltsopensource.tasktracker.Result;
import com.github.ltsopensource.tasktracker.runner.JobContext;
import com.github.ltsopensource.tasktracker.runner.JobRunner;
import com.quanyan.assets.scheduler.AssetsTask;
import com.quanyan.assets.service.UtilAccumulateService;
import com.quanyan.assets.service.UtilCouponService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @author wmb
 */
public class AssetsJobRunner implements JobRunner {

    private static final Logger logger = LoggerFactory.getLogger(AssetsJobRunner.class);

    @Autowired
    private UtilCouponService utilCouponService;

    @Autowired
    private UtilAccumulateService utilAccumulateService;

    @Override
    public Result run(JobContext jobContext) throws Throwable {
        logger.debug("------TaskJobRunner执行了---------" + jobContext);
        Job job = jobContext.getJob();//对于需要执行多个任务的业务场景，可以使用这种方式
        String jobType = job.getParam("type");
        //订单关闭
        if (AssetsTask.COUPON_EXPIRED.equals(jobType)) {
            try {
                //过期逻辑
                utilCouponService.expiredHandle();
                return new Result(Action.EXECUTE_SUCCESS, "--"+AssetsTask.COUPON_EXPIRED+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+AssetsTask.COUPON_EXPIRED+"--task-execute-fail--->"+e.getMessage());
            }
        }else if(AssetsTask.EGGS_PHONE_NICKNAME.equals(jobType)){
            try {
                //过期逻辑
                utilAccumulateService.repairPhoneNickName();
                return new Result(Action.EXECUTE_SUCCESS, "--"+AssetsTask.COUPON_EXPIRED+"---task-execute-success----");
            } catch (Exception e) {
                logger.info("Run job failed!", e);
                return new Result(Action.EXECUTE_LATER, "--"+AssetsTask.COUPON_EXPIRED+"--task-execute-fail--->"+e.getMessage());
            }
        }else{
            return new Result(Action.EXECUTE_SUCCESS, "-------no execute task---------------");
        }
    }

}
